package bioinspired.routing.graph;

import java.io.*;
import java.util.*;

import org.jgraph.graph.DefaultEdge;
import org.jgrapht.*;
import org.jgrapht.graph.DefaultDirectedGraph;


/**
 * Class Comments
 * 
 * This class is the base for the routing algorithms. It creates the graph from a text file with adjacency matrix
 * representation. In order to create the graph, the JGraphT (http://jgrapht.org/) library is used. 
 * @author yeison
 *
 */
public class Graph {
	
	/**
	 * The constructor the Graph class. It creates a directed graph from a file with and adjacency matrix representation
	 * of it. This must be the first class called when performing bio-inspired routing. Based on the graph created
	 * the routing algorithm will create the routing tables and execute the routing.
	 * @param pathToFile 
	 * The name of the path in the SO to the file to be loaded. It has to be an absolute path. 
	 * The file must be a text file which contains a matrix to represent the graph (in the adjacency matrix form).  
	 */
	//Constructor
	public Graph (String pathToFile) {
		this.pathToFile = pathToFile;
		generateGraphFromFile();
	}
	
	
	public org.jgrapht.Graph<Long, DefaultEdge> getGraph () {
		return g;
	}
	
	
	
	
	
	
	private void generateGraphFromFile() {
		int i = 0;
        int j = 0;
        try {
            FileInputStream fstream = new FileInputStream(pathToFile);
            FileInputStream fstream2 = new FileInputStream(pathToFile);
            DataInputStream in = new DataInputStream(fstream);
            DataInputStream in2 = new DataInputStream(fstream2);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
            String strLine;
            String line = br2.readLine();
            StringTokenizer nodeCount = new StringTokenizer(line, " ");
            adjacencyMatrix = new int[nodeCount.countTokens()][nodeCount.countTokens()];
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                //System.out.println(strLine);
                StringTokenizer nodeId = new StringTokenizer(strLine, " ");
                //System.out.println(nodeId.countTokens());
                int nodes = nodeId.countTokens();
                
                while (nodeId.hasMoreTokens()) {
                    String digits = nodeId.nextToken();
                    adjacencyMatrix[i][j] = Integer.parseInt(digits.trim());
                    j++;
                    //System.out.println(Integer.parseInt(digits.trim()));
                    //System.out.println("M: " + adjacencyMatrix[i][j]);                    
                }
                i++;
                j = 0;
            }
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
        //vertices = new Object[adjacencyMatrix.length]; ***************
        
//        System.out.println("Topology Matrix: ");
//        for (int k = 0; k < adjacencyMatrix.length; k++) {
//            for (int l = 0; l < adjacencyMatrix[k].length; l++) {
//                System.out.print(adjacencyMatrix[k][l] + " ");
//            }
//            System.out.println();
//        }
        
        for (int k = 0; k < adjacencyMatrix.length; k++) {
            g.addVertex((long) k);
        }
        for (int k = 0; k < adjacencyMatrix.length; k++) {
            for (int l = 0; l < adjacencyMatrix[k].length; l++) {
                if (adjacencyMatrix[k][l] == 1) {
                    g.addEdge((long) k, (long) l);
                }
            }
            
        }
		
	}







	//Instance Variables
	private String pathToFile;
	private org.jgrapht.Graph<Long, DefaultEdge> g = new DefaultDirectedGraph<Long, DefaultEdge>(DefaultEdge.class);
	private int[][] adjacencyMatrix;
	

}
